const NewsModel = require('../../models/newsModel');

const NewsService = {
    //创建内容
    add: async ({ title, content, category, isPublish, cover, editTime, author }) => {
        return NewsModel.create({
            title,
            content,
            category,
            isPublish,
            cover,
            editTime,
            author,
        });
    },
    //获取单个用户的列表
    postList: async ({ author }) => {
        return NewsModel.find({ author });
    },
    //获取列表
    getList: async ({ _id }) => {
        return _id ? NewsModel.findOne({ _id }) : NewsModel.find({});
    },
    //修改发布状态
    publish: async ({ _id, isPublish, editTime }) => {
        return NewsModel.updateOne({ _id }, { isPublish: Number(isPublish), editTime });
    },
    // 删除文章
    delList: async ({ _id }) => {
        return NewsModel.deleteOne({ _id });
    },
    //更新文章
    updateList: async ({ _id, title, content, category, isPublish, cover, editTime }) => {
        if (cover) {
            return NewsModel.updateOne(
                { _id },
                {
                    title,
                    content,
                    category,
                    isPublish,
                    cover,
                    editTime,
                }
            );
        } else {
            return NewsModel.updateOne(
                { _id },
                {
                    title,
                    content,
                    category,
                    isPublish,
                    editTime,
                }
            );
        }
    },
};

module.exports = NewsService;
